<template>
  <div class="mod-config">
    <el-form :inline="true" :model="dataForm" @keyup.enter.native="getDataList()">
      <el-form-item>
        <el-input v-model="dataForm.key" placeholder="参数名" clearable></el-input>
      </el-form-item>
      <el-form-item>
        <el-button @click="getDataList()">查询</el-button>
      </el-form-item>
    </el-form>
    <el-table
        :data="dataList"
        border
        v-loading="dataListLoading"
        @selection-change="selectionChangeHandle"
        style="width: 100%;"
        class="table">
      <el-table-column
          prop="id"
          header-align="center"
          align="center"
          label="id">
      </el-table-column>
      <!--      <el-table-column-->
      <!--        prop="inspectorName"-->
      <!--        header-align="center"-->
      <!--        align="center"-->
      <!--        label="安全检查员">-->
      <!--      </el-table-column>-->
      <el-table-column
          prop="inspectionDate"
          header-align="center"
          align="center"
          label="安全检查时间">
      </el-table-column>
      <el-table-column
          prop="houseLocation"
          header-align="center"
          align="center"
          label="检查的房屋">
      </el-table-column>
      <el-table-column
          prop="score"
          header-align="center"
          align="center"
          label="检查的得分">
        <template slot-scope="scope">
          {{ scope.row.score.toFixed(2) }}
        </template>
      </el-table-column>
      <el-table-column
          prop="photo"
          header-align="center"
          align="center"
          label="安全检查的照片">
        <template slot-scope="scope">
          <el-image :src="scope.row.photo" :preview-src-list="[scope.row.photo]"></el-image>
        </template>
      </el-table-column>
      <el-table-column
          fixed="right"
          header-align="center"
          align="center"
          width="150"
          label="操作">
        <template slot-scope="scope">
          <el-button type="text" size="small" @click="addOrUpdateHandle(scope.row.id)">详情</el-button>
        </template>
      </el-table-column>
    </el-table>
    <!--手机端显示-->
    <div class="cards">
      <el-card
          class="card"
          v-for="data in dataList"
          :key="data.id"
          @click.native="addOrUpdateHandle(data.id)">
        <div class="title">
          检查记录{{data.id}}号-{{data.houseLocation}}
        </div>
        <span class="content">
          {{data.score.toFixed(2)}}
        </span>
        <span class="content">
          {{ data.inspectionDate}}
        </span>
<!--        <i class="el-icon-remove remove-icon" @click.stop="deleteHandle(data.id)" />-->
      </el-card>
    </div>
    <el-pagination
        @size-change="sizeChangeHandle"
        @current-change="currentChangeHandle"
        :current-page="pageIndex"
        :page-sizes="[10, 20, 50, 100]"
        :page-size="pageSize"
        :total="totalPage"
        layout="total, sizes, prev, pager, next, jumper">
    </el-pagination>
    <!-- 弹窗, 详情 -->
    <detail v-if="addOrUpdateVisible" ref="addOrUpdate" @refreshDataList="getDataList"></detail>
  </div>
</template>

<script>
import Detail from './inspection-details.vue'

export default {
  /* eslint-disable */
  name: 'inspection',
  data() {
    return {
      loginUser: JSON.parse(localStorage.getItem('user')) || {},
      dataForm: {
        key: ''
      },
      dataList: [],
      pageIndex: 1,
      pageSize: 10,
      totalPage: 0,
      dataListLoading: false,
      dataListSelections: [],
      addOrUpdateVisible: false
    }
  },
  components: {
    Detail
  },
  mounted() {
    this.getDataList()
    //若从消息页面跳转，则打开对应检查记录详情
    const id = this.$route.query.id || 0
    if (id === 0) {
      return
    }
    this.addOrUpdateHandle(id)
  },
  methods: {
    // 获取数据列表
    getDataList() {
      this.dataListLoading = true
      // const url = this.loginUser.role === '业主' ? '/inspection/inspection/getOwnerInspection' : '/inspection/inspection/getCurrentUserInspection'
      this.$request({
        url: '/inspection/inspection/getCurrentUserInspection',
        method: 'get',
        params: {
          'page': this.pageIndex,
          'limit': this.pageSize,
          'key': this.dataForm.key
        }
      }).then((res) => {
        if (res && res.code === 200) {
          this.dataList = res.data.list
          this.totalPage = res.data.totalCount
        } else {
          this.dataList = []
          this.totalPage = 0
        }
        this.dataListLoading = false
      })
    },
    // 每页数
    sizeChangeHandle(val) {
      this.pageSize = val
      this.pageIndex = 1
      this.getDataList()
    },
    // 当前页
    currentChangeHandle(val) {
      this.pageIndex = val
      this.getDataList()
    },
    // 多选
    selectionChangeHandle(val) {
      this.dataListSelections = val
    },
    // 新增 / 修改
    addOrUpdateHandle(id) {
      this.addOrUpdateVisible = true
      this.$nextTick(() => {
        this.$refs.addOrUpdate.init(id)
      })
    },
    // 删除
    deleteHandle(id) {
      var ids = id ? [id] : this.dataListSelections.map(item => {
        return item.id
      })
      this.$confirm(`确定对[id=${ids.join(',')}]进行[${id ? '删除' : '批量删除'}]操作?`, '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        this.$request({
          url: '/inspection/inspection/delete',
          method: 'post',
          data: ids
        }).then((res) => {
          if (res && res.code === 200) {
            this.$message({
              message: '操作成功',
              type: 'success',
              duration: 1500,
              onClose: () => {
                this.getDataList()
              }
            })
          } else {
            this.$message.error(res.msg)
          }
        })
      })
    }
  }
}
</script>

<style scoped>
.cards {
  display: none;
}

.card {
  margin: 0.5rem 0;
}

.title {
  font-size: 1rem;
  font-weight: bold;
  margin-bottom: 0.5rem;
}

.content {
  margin: 0 0.2rem;
}

.remove-icon {
  color: red;
  font-size: 1.6rem;
  float: right;
  margin: auto 0;
}

@media (max-width: 700px) {
  .cards {
    display: block;
  }
  .table {
    display: none;
  }
}
</style>
